package com.suoluo.zenadmin.dao.account;

import java.util.List;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;

import com.suoluo.zenadmin.vo.account.AccountVO;
import com.suoluo.zenadmin.vo.account.OpenAccountVO;

@DAO
public interface AccountDAO {

	@ReturnGeneratedKeys
	@SQL("insert into tb_account(t_account_name,t_account_head,t_account_gender,t_account_create_time) values (" +
			":1.accountName,:1.accountHead,:1.accountGender,:1.accountCreateTime)")
	public int saveAccount(AccountVO account);
	
	@ReturnGeneratedKeys
	@SQL("insert into tb_open_account(t_open_id,t_token,t_token_expire,t_account_id,t_open_type,t_domain_name,t_screen_name) values(" +
			":1.openId,:1.token,:1.tokenExire,:1.accountId,:1.openType,:1.domainName,:1.screenName)")
	public int saveOpenAccount(OpenAccountVO openAccount);
	
	

	@SQL("select * from tb_account  where t_account_id = :1")
	public AccountVO findAccount(int accountId);
	
	@SQL("select * from tb_open_account where t_account_id = :1")
	public List<OpenAccountVO> findOpenAccount(int accountId);
	
	@SQL("select * from tb_open_account where t_open_type = :1 and t_open_id = :2")
	public OpenAccountVO findOpenAccount(int openType,String openId);
	
	@SQL("update tb_open_account set t_open_id=:1.openId,t_token=:1.token,t_token_expire=:1.tokenExpire," +
			"t_account_id=:1.accountId,t_open_type=:1.openType,t_domain_name=:1.domainName," +
			"t_screen_name=:1.screenName where t_open_account_id=:1.openAccountId")
	public void updateOpenAccount(OpenAccountVO openAccount);
	
	@SQL("update tb_account set t_account_name = :1.accountName,t_account_head=:1.accountHead," +
			"t_account_gender=:1.accountGender where t_account_id = :1.accountId")
	public void updateAccount(AccountVO account);
	
	@SQL("delete from tb_open_account where t_open_account_id=:1")
	public void deleteOpenAccount(int openAccountId);
	
	@SQL("delete from tb_account where t_account_id=:1")
	public void deleteAccount(int accountId);
	
	@SQL("select * from tb_open_account where t_screen_name=:1 and t_open_type=1")
	public OpenAccountVO findOpenAccountByName(String userScreen);
	
//	class AccountJoinOpenAccountMapper implements RowMapper<AccountPOJO>{
//
//		@Override
//		public AccountPOJO mapRow(ResultSet rs, int rowNum) throws SQLException {
//			AccountPOJO account = new AccountPOJO();
//			int accountId = rs.getInt("t_account_id");
//			account.setAccountId(accountId);
//			String accountName = rs.getString("t_account_name");
//			account.setAccountName(accountName);
//			String accountHead
//			return account;
//		}
//		
//	}

//	public TbAccount queryAccount(int accountId){
//		String sql = "select  a.t_account_id,a.t_account_name,a.t_account_head," +
//				"oa.t_open_id,oa.t_token,oa.t_token_expire,oa.t_open_type,oa.t_open_account_id " +
//				"from tb_account a join tb_open_account oa " +
//				"ON a.t_account_id = :accountId";
//		TransactionManager tm = super.getTransactionManager();
//		ReadonlyTranSession session = tm.openDelayReadTran();
//		try {
//			CompiledSQL com = tm.getCompiledSQLBuilder().buildCompiledSQL(
//					TbAccount.class, sql);
//			BindedCompiledSQL bcs = com.bindNoParams();
//			bcs.bind("accountId", accountId);
//			bcs.setBeanMapRowDataLoader(HashMap.class);
//			TbAccount account = null;
//			List<Map> results = session.list(bcs);
//			int count = 0;
//			for (Map row : results){
//				if (count == 0){
//					account = new TbAccount();
//					account.setAccountId((Integer) row.get("accountId"));
//					account.setAccountName((String) row.get("accountName"));
//					account.setAccountHead((String) row.get("accountHead"));
//				}
//				
//				TbOpenAccount openAccount =  new TbOpenAccount();
//				openAccount.setOpenAccountId((Integer) row.get("t_open_account_id"));
//				openAccount.setOpenId((String) row.get("t_open_id"));
//				openAccount.setOpenType((Integer) row.get("t_open_type"));
//				openAccount.setToken((String) row.get("t_token"));
//				openAccount.setTokenExpire((Date) row.get("t_token_expire"));
//				account.addOpenAccount(openAccount); 
//				count++;
//			}
//			return account;
//		} finally {
//			session.close();
//		}
//	}
}
